package cn.tedu.charging.user.utils;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author
 * @date 0004
 */
@Component
public class AlipayUtil {

    /**
     * 支付宝支付的appid 类似于微信小程序的appid 确定唯一身份
     */
    @Value("${alipay.appid}")
    private String appid;
    /**
     * 支付的时候调用 支付宝的支付地址
     */
    @Value("${alipay.url}")
    private String url;

    /**
     * 为了保证信息安全 加密
     */
    @Value("${alipay.privateKey}")
    private String privateKey;
    @Value("${alipay.publicKey}")
    public String publicKey;

    /**
     * 通知的url
     */
    @Value("${alipay.notifyUrl}")
    private String notifyUrl;

    /**
     * 支付成功后的url 要调用的页面
     */
    @Value("${alipay.returnUrl}")
    private String returnUrl;

    /**
     * 调用支付宝支付框架进行支付
     * @param id
     * @param price
     * @param title
     * @return
     */
    public String pay(String id, String price, String title){
        AlipayClient alipayClient =
                new DefaultAlipayClient(url,appid,privateKey,"json","UTf-8",publicKey,"RSA2");
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        request.setNotifyUrl(notifyUrl);
        request.setReturnUrl(returnUrl);
        JSONObject bizContent = new JSONObject();
        //唯一的支付流水号 重要
        bizContent.put("out_trade_no", id);
        //金额 重要
        bizContent.put("total_amount", price);
        //标题商品
        bizContent.put("subject", title);
        //商品code
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");

        request.setBizContent(bizContent.toString());
        AlipayTradePagePayResponse response = null;
        String form = null;
        try {
            response = alipayClient.pageExecute(request);
            form = response.getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return form;
    }

    public String query(String id){
        AlipayClient alipayClient = new DefaultAlipayClient(url,appid,privateKey,"json","UTf-8",publicKey,"RSA2");
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", id);
        request.setBizContent(bizContent.toString());
        AlipayTradeQueryResponse response = null;
        String body = null;
        try {
            response = alipayClient.execute(request);
             body = response.getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return body;
    }
}
